package org.elephas.webapp.backend.component;

import java.io.Serializable;
import java.util.Arrays;

import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableChoiceLabel;
import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableLabel;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 * Renders an ajax editable label/choice instead of a plain label.
 * 
 * FIXME: changes are not persisted. do we need to override onModelChanged in
 * Detachable*Model and save the entity there? Or just add a save button to the
 * row?
 * 
 * @author Gerolf Seitz
 * 
 */
public class EditablePropertyColumn extends PropertyColumn {
	private static final long serialVersionUID = 1L;

	public EditablePropertyColumn(IModel displayModel, String propertyExpression) {
		super(displayModel, propertyExpression);
	}

	@Override
	public void populateItem(Item item, String componentId, IModel model) {
		IModel labelModel = createLabelModel(model);
		Class<?> c = labelModel.getObject().getClass();
		if (c.isEnum()) {
			item.add(new AjaxEditableChoiceLabel(componentId, labelModel,
					new Model((Serializable) Arrays
							.asList(c.getEnumConstants()))));
		} else {
			item.add(new AjaxEditableLabel(componentId, labelModel));
		}
	}

}
